package com.e.takeaway;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.e.takeaway.adapter.FoodAdapter;
import com.e.takeaway.po.Food;
import com.e.takeaway.po.User;
import com.e.takeaway.service.RequestService;
import com.e.takeaway.util.MyRetrofit;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;


public class LoginActivity extends AppCompatActivity {
    private EditText ed_name,ed_password,ed_ver;
    private Button btn_login;
    final String FILE_NAME="user";
    ImageView im_ver;
    private static User user = new User();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        SharedPreferences sp = getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        String phone = sp.getString("phone","");
        String nick = sp.getString("nickname","");
        if ("".equals(phone) || "".equals(nick)){
            InitView();
        }else {
            System.out.println("login: ----------------" + phone);
            Intent intent = new Intent(LoginActivity.this,MainActivity.class);
            startActivity(intent);
            finish();
        }
    }


    public void InitView(){
        im_ver = (ImageView)findViewById(R.id.im_ver);
        double x = (Math.random()*9+1)*100000;
        int a;
        a = (int) x;
        Picasso.with(this)
                .load("http://124.222.71.97/android/code?tokenId="+a)
                .placeholder(im_ver.getDrawable())
                .error(R.drawable.lunbo1)
                .into(im_ver);
        im_ver.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                double x = (Math.random()*9+1)*100000;
                int a;
                a = (int) x;
                Picasso.with(view.getContext())
                        .load("http://124.222.71.97/android/code?tokenId="+a)
                        .placeholder(im_ver.getDrawable())
                        .error(R.drawable.lunbo1)
                        .into(im_ver);
            }
        });
        ed_name = (EditText) findViewById(R.id.username);
        ed_ver = (EditText)findViewById(R.id.ed_ver);
        ed_password = (EditText) findViewById(R.id.password);
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String phone = ed_name.getText().toString();
                    if (phone.length()!=11){
                        Toast.makeText(LoginActivity.this,"手机格式不正确",Toast.LENGTH_SHORT).show();
                    }
                    if(ed_ver.getText().toString().equals("")){
                        Toast.makeText(LoginActivity.this,"请输入验证码",Toast.LENGTH_SHORT).show();
                    }
                    if (!ed_ver.getText().toString().equals(String.valueOf(a))){
                        Toast.makeText(LoginActivity.this,"验证码错误",Toast.LENGTH_SHORT).show();
                        Picasso.with(view.getContext())
                                .load("http://124.222.71.97/android/code?tokenId="+a)
                                .placeholder(im_ver.getDrawable())
                                .error(R.drawable.lunbo1)
                                .into(im_ver);
                    }
                    else{
                        String pwd = ed_password.getText().toString();
                        requestData(phone, pwd);
                    }

                    //  System.out.println(user.toString());
                }
            });
        }
    private void requestData(String  phone,String password) {
        Retrofit retrofit = MyRetrofit.getRetrofit();
        RequestService request = retrofit.create(RequestService.class);
        Call<User> call = request.getUser(phone,password);
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                user = response.body();
                System.out.println(user);
                if(user!=null){
                    SharedPreferences sharedPreferences = getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("phone",phone);
                    editor.putString("nickname",user.getNickname());
                    editor.putString("pwd",user.getPassword());
                    editor.commit();
                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                }else{
                    Toast.makeText(LoginActivity.this,"账号或密码错误!",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                System.out.println("用户请求失败-------------------》error");
            }
        });
    }
}